<?php

namespace app\models;

use yii\behaviors\TimestampBehavior;

/**
 * @property int $uid
 * @property int $group_id
 * @property string $group_path
 * @property int $dept_id
 * @property string $dept_path
 * @property int $position_id
 * @property string $real_name
 * @property string $mobile
 * @property string $avatar
 * @property int $sex
 * @property string $email
 * @property int $province_id
 * @property int $city_id
 * @property int $district_id
 * @property string $street
 * @property int $birthday
 * @property string $qq
 * @property int $updated_at
 *
 * @property ActiveRecord $group
 * @property ActiveRecord $dept
 */
class UserProfile extends ActiveRecord
{

    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::class,
                'createdAtAttribute' => false
            ]
        ];
    }

    public function getGroup()
    {
        return $this->hasOne(Group::class, ['id' => 'group_id']);
    }

    public function getDept()
    {
        return $this->hasOne(Department::class, ['id' => 'dept_id']);
    }

    public function rules()
    {
        return [
            [
                [
                    'uid',
                    'group_id',
                    'group_path',
                    'dept_id',
                    'dept_path',
                    'position_id',
                    'real_name',
                    'mobile',
                    'avatar',
                    'sex',
                    'email',
                    'province_id',
                    'city_id',
                    'district_id',
                    'street',
                    'birthday',
                    'qq',
                ],
                'safe'
            ]
        ];
    }

    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            if ($insert) {
                $this->birthday = strtotime($this->birthday);
            }
            return true;
        }
        return false;
    }

    public static function tableName()
    {
        return '{{%user_profile}}';
    }

    /**
     * 查询用户信息
     * @param $id
     * @param string[] $column
     * @return array|null
     */
    public static function getInfo($id, $column = ['*'])
    {
        $query = self::find();
        $query->select($column);
        $query->where(['uid' => $id]);
        $query->asArray();
        return $query->one();
    }
}